(()=> {
	let ele = {};
	ele.listBox1 = $('#listBox1');
	ele.listBox2 = $('#listBox2');
	ele.listBox3 = $('#listBox3');
	ele.close_detail = $('#close-detail');
	ele.modal_detail = $('#modal-detail');
	ele.renming = $('#renming');
	ele.listDeail = $('#listDeail');
	ele.detailnum1 = $('#detailnum1');
	ele.detailnum2 = $('#detailnum2');
	ele.detailgold1 = $('#detailgold1');
	ele.detailgold2 = $('#detailgold2');
	ele.jinjie = $('#jinjie');
	ele.detailin = $('#detailin');
	ele.jinjie1 = $('#jinjie1');
	ele.jinjie2 = $('#jinjie2');
	ele.jinjieimg = $('#jinjieimg');
	
	
	//关闭 人才详情
	ele.close_detail.onclick = () => {
		closeMdl(ele.modal_detail);
	}
	
	let is_jinjie = '';
	
	shopList();
	// 人才列表
	function shopList() {
		let token = getCookie("token");
		ajax({
			url: API_PATH.SHOP_LIST,
			method: 'GET',
			header: {
				'Authorization': token,
			},
			success: res => {
				if (res.code == 1) {
					//任命人数
					ele.renming.innerHTML = '任命人数：' + res.data.rm_txt
					drawList(res.data)
				} else openTips(res.msg || '未知错误');
			}
		});
	}
	
	// 人才详情
	function shopDetail(index) {
		let token = getCookie("token");
		ajax({
			url: API_PATH.SHOP_DETAIL,
			method: 'GET',
			header: {
				'Authorization': token,
			},
			data: {
				id: index
			},
			success: res => {
				if (res.code == 1) {
					openDetail(res.data);
				} else openTips(res.msg || '未知错误');
			}
		});
	}
	
	// 人才招募和进阶
	function synthesisUp(index,jinjie) {
		let token = getCookie("token");
		ajax({
			url: API_PATH.SYNTHESIS_UP,
			method: 'POST',
			header: {
				'Authorization': token,
			},
			data: {
				id: index
			},
			success: res => {
				if (res.code == 1) {
					openTips(res.msg);
					if(jinjie) {
						shopDetail(index);
					}
					shopList();
				} else openTips(res.msg || '未知错误');
			}
		});
	}
	
	function drawList(_data) {
		let html1 = '';
		let html2 = '';
		let html3 = '';
		let pj_type = '';
		let zhaomu = '';
		
		for(let i = 0; i < _data.sz_talent.length; i++) {
			// console.log(_data.sz_talent[i].class.pj_type);
			pj_type = _data.sz_talent[i].pj_type == 1 ? 'optiongreen' : _data.sz_talent[i].pj_type == 2 ? 'optionzise' : 'optionorange';
			html1 += '<div class="list '+pj_type+'" data="'+_data.sz_talent[i].shop.id+'">'
					 	+'<div class="listin" data="'+_data.sz_talent[i].shop.id+'">'
					 		+'<img src="'+IMG_DOMAIN+_data.sz_talent[i].shop.logo+'" alt="" data="'+_data.sz_talent[i].shop.id+'">'
					 		+'<div class="apart"  data="'+_data.sz_talent[i].shop.id+'">'
					 			+'<img src="http://img.qxgs.cc/tiantianlaishouzu/img/icon/'+_data.sz_talent[i].class.id+'.png" alt=""  data="'+_data.sz_talent[i].shop.id+'">'
					 			+'<span data="'+_data.sz_talent[i].shop.id+'">'+_data.sz_talent[i].class.jz_name+'</span>'
					 		+'</div>'
					 	+'</div>'
					 	+'<span class="green" data="'+_data.sz_talent[i].shop.id+'">'+_data.sz_talent[i].shop.name+'</span>'
					 +'</div>';
		}
		ele.listBox1.innerHTML = html1;
		
		for(let i = 0; i < _data.kx_talent.length; i++) {
			pj_type = _data.kx_talent[i].pj_type == 1 ? 'optiongreen' : _data.kx_talent[i].pj_type == 2 ? 'optionzise' : 'optionorange';
			html2 += '<div class="list '+pj_type+'" data="'+_data.kx_talent[i].shop.id+'">'
					 	+'<div class="listin" data="'+_data.kx_talent[i].shop.id+'">'
					 		+'<img src="'+IMG_DOMAIN+_data.kx_talent[i].shop.logo+'" alt="" data="'+_data.kx_talent[i].shop.id+'">'
					 		+'<div class="apart" data="'+_data.kx_talent[i].shop.id+'">'
					 			+'<img src="http://img.qxgs.cc/tiantianlaishouzu/img/icon/'+_data.kx_talent[i].class.id+'.png" alt="" data="'+_data.kx_talent[i].shop.id+'">'
					 			+'<span data="'+_data.kx_talent[i].shop.id+'">'+_data.kx_talent[i].class.jz_name+'</span>'
					 		+'</div>'
					 	+'</div>'
					 	+'<span class="green" data="'+_data.kx_talent[i].shop.id+'">'+_data.kx_talent[i].shop.name+'</span>'
					 +'</div>';
		}
		ele.listBox2.innerHTML = html2;
		
		for(let i = 0; i < _data.list.length; i++) {
			pj_type = _data.list[i].type == 1 ? 'optiongreen' : _data.list[i].type == 2 ? 'optionzise' : 'optionorange';
			zhaomu = _data.list[i].has_num >= _data.list[i].num ? '<button class="zhaomu" data2="'+_data.list[i].id+'">招募</button>' : ''
			html3 += '<div class="list '+pj_type+'" data="'+_data.list[i].id+'">'
					 	+'<div class="listin" data="'+_data.list[i].id+'">'
					 		+'<img src="'+IMG_DOMAIN+_data.list[i].logo+'" alt="" data="'+_data.list[i].id+'">'
							+'<div class="suipian" data="'+_data.list[i].id+'">'
								+'<p data="'+_data.list[i].id+'">碎片</p>'
								+'<p data="'+_data.list[i].id+'">'+_data.list[i].has_num+'/'+_data.list[i].num+'</p>'
							+'</div>'
					 		+'<div class="apart" data="'+_data.list[i].id+'">'
					 			+'<img src="http://img.qxgs.cc/tiantianlaishouzu/img/icon/'+_data.list[i].class.id+'.png" alt="" data="'+_data.list[i].id+'">'
					 			+'<span data="'+_data.list[i].id+'">'+_data.list[i].class.jz_name+'</span>'
					 		+'</div>'
							+zhaomu
					 	+'</div>'
					 	+'<span class="green" data="'+_data.list[i].id+'">'+_data.list[i].name+'</span>'
					 +'</div>';
		}
		ele.listBox3.innerHTML = html3;
		
		//详情
		let lists = $('.list');
		for(let i = 0; i < lists.length; i ++) {
			lists[i].onclick = (e) => {
				// console.log(e.target.getAttribute('data'));
				shopDetail(e.target.getAttribute('data'));
			}
		}
		
		//招募
		let zhaomus = $('.zhaomu');
		for(let i = 0; i < zhaomus.length; i ++) {
			zhaomus[i].onclick = (e) => {
				// console.log(e.target.getAttribute('data'));
				synthesisUp(e.target.getAttribute('data2'));
			}
		}
	}
	
	// 打开人才详情 弹窗
	function openDetail(_data) {
		let pj_type = '';
		let logo = '';
		if( _data.record !== null) {
			pj_type = _data.record.pj_type == 1 ? 'optiongreen' : _data.record.pj_type == 2 ? 'optionzise' : 'optionorange';
		} else {
			pj_type = _data.info.hammer == 2 ? 'optiongreen' : _data.info.hammer == 4 ? 'optionzise' : 'optionorange';
		}
		logo = IMG_DOMAIN+_data.logo
		ele.listDeail.innerHTML = '<div class="list '+pj_type+'">'
									+'<div class="listin">'
										+'<img src="'+logo+'" alt="">'
										+'<div class="apart">'
											+'<img src="http://img.qxgs.cc/tiantianlaishouzu/img/home/aprt.png" alt="">'
											+'<span>'+_data.class.jz_name+'</span>'
										+'</div>'
									+'</div>'
									+'<span class="green">'+_data.name+'</span>'
									
		ele.detailnum1.innerHTML = _data.has_num;
		ele.detailnum2.innerHTML = _data.record == null ? '/' +  _data.num : '/' +  _data.jj_num;
		ele.detailnum2.style.display = _data.record != null && _data.record.type == 3 ? 'none' : 'block';
		ele.jinjie1.style.display = _data.record == null ? 'none' : 'block';
		ele.jinjie2.style.display = _data.record == null ? 'none' : 'block';
		ele.jinjieimg.style.display = _data.record == null ? 'none' : 'block';
		
		ele.detailgold1.innerHTML = '金币获取：' +  _data.money + '个';
		ele.detailgold2.innerHTML = '金币获取：' +  _data.jj_money + '个';
		console.log(_data.record != null);
		if(_data.record != null) {
			ele.jinjie.className = 'jinjie ' + _data.id;
			ele.jinjie.style.display = 'block';
		}
		//进阶
		let jinjies = $('.jinjie');
		for(let i = 0; i < jinjies.length; i ++) {
			jinjies[i].onclick = (e) => {
				// console.log(e.target.className.split(' ')[1]);
				synthesisUp(e.target.className.split(' ')[1],1);
			}
		}
		// if(!index) {
			openMdl(ele.modal_detail);
		// }
	}
	
})()